'use strict';

const Service = require('egg').Service;

class LoginService extends Service {
    async login(params) {
        // console.log('----------------ccccccccccccccc-----------------------');
        // console.log(this.ctx.request.headers);
        const result = await this.ctx.curl(params, {
            dataType: 'json',
        });
        // console.log(params);
        // console.log(result.status, result.headers, result.data);
        // const result = { portal_key: 'PORTAL-ST-6c240679-2d03-4f1f-9a27-fced23ae26ed', status: 'success' };
        return result;
    }
    async request(api, opts) {
        // this.ctx.headers.portal_key = this.ctx.session.portal_key;
        // console.log(opts);
        if (opts.headers && Object.getOwnPropertyNames(opts.headers).length > 0) {
            opts.headers.portal_key = this.ctx.session.portal_key;
        } else {
            opts.headers = { portal_key: this.ctx.session.portal_key };
        }
        // console.log(this.ctx.session.portal_key + '789789789');
        const isture = await this.ctx.curl(`http://84.16.9.28:8080/portal/sso/validateKey.c?portal_key=${this.ctx.session.portal_key}&service=${encodeURIComponent('http://84.16.9.28:8080' + this.ctx.path)}`, {
            dataType: 'json',
        });
        // console.log(isture);
        if (false) {
            // await this.getTicket({ service: encodeURIComponent('http://84.16.9.28:8080' + this.ctx.path) });
            this.ctx.session = null;
            this.ctx.redirect(`http://84.16.9.28:8080/portal/sso/login.do?service=${url}`);
        } else {
            const result = await this.ctx.curl(api, opts);
            return result;
        }
    }

    async getUserInfo() {
        const URL = 'http://84.12.68.101:7010/api/zpdksq';
        const result = await this.request(`${URL}/getuserinfo`, {
            dataType: 'json',
        });
        return result.data;
    }


}

module.exports = LoginService;